#!/usr/bin/python
# -*- coding: UTF-8 -*-


import time,threading,sys
sys.path.append("/home/biowin/Minibot/04.RobotSamples/03.SpiderRobot")
# sys.path.append("../../")
from BwRobotLib import BwRobotLib
from SpiderRobot import SpiderRobot
from BwSensorLib import BwSensorLib
import os,inspect

# 加载机器人构型配置
fileName = inspect.getframeinfo(inspect.currentframe()).filename
folderPath     = os.path.dirname(os.path.abspath(fileName))
cfile=folderPath+"/config.txt"


class GestureRobot:
	def __init__(self):
		# 实例化传感器库对象
		self.sensorlib = BwsensorliblibLib("udp")
		# 实例化BwRobotLib库
		self.robotlib = BwRobotLib("udp")
		# 实例化蜘蛛机器人库
		self.spider = SpiderRobot(self.robotlib)
		# 连接机器人设备
		self.robotlib.connectRobot("10.10.100.254",8899) #真机AP模式
        # 导入机器人配置参数
        self.robotlib.loadConfig(config_file)
		#配置ID
		self.spider.bindIds(self.robotlib.module_ids)
		# 点阵显示
		# 点阵图案：向上箭头 向下箭头　向左箭头　向右箭头 　
		self.display_data = [[0x08,0x1C,0x2A,0x08,0x08],[0x08,0x08,0x2A,0x1C,0x08],[0x08,0x10,0x3F,0x10,0x08],[0x08,0x04,0x7E,0x04,0x08]]
	
	def gestureRobotMotion(self):
		while 1:
			gesture = self.sensor.readGestureSensor() # 读取手势传感器数据
			print ("返回值：",gesture)
			# 检测到向上手势
			if(gesture == 1):
				print("向前走")
				self.sensorlib.setMatrixSensor(self.display_data[10]) # 点阵显示向上箭头
				self.spider.goForward()
			# 检测到向下手势
			if(gesture == 2):
				print("向后走")
				self.sensorlib.setMatrixSensor(self.display_data[11]) # 点阵显示向上下箭头
				self.spider.goBack()
			# 检测到向左手势
			if(gesture == 3):
				print("向左转向")
				self.sensorlib.setMatrixSensor(self.display_data[12]) # 点阵显示向左箭头
				self.spider.turnLeft()
			# 检测到向右手势
			if(gesture == 4):
				print("向右转向")
				self.sensorlib.setMatrixSensor(self.display_data[13]) # 点阵显示向右箭头
				self.spider.turnRight()
			
			
# 主函数
def main():
    # 实例化手势控制类
    gestureRobot = GestureRobot()
    # 执行手势控制功能
    gestureRobot.gestureRobotMotion()


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print(" Program closed! ")
    pass
